package com.own.business.push.module.xiaomi.client;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.own.business.push.core.base.BasePushClientMethod;
import com.own.business.push.core.model.MessagePushContentModel;
import com.own.business.push.module.xiaomi.property.PushXiaomiProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PushXiaomiClient
 *
 * @author chenxueli
 * @date 2023-11-15 19:52:00
 */
@Slf4j
@Component
public class PushXiaomiClient implements BasePushClientMethod {

    @Resource
    private PushXiaomiProperty pushXiaomiProperty;

    /**
     * 全量推送
     *
     * @param contentModel 内容
     */
    @Override
    public void all(MessagePushContentModel contentModel) {
        var content = buildContent(contentModel);
        var request = HttpRequest.post("https://api.xmpush.xiaomi.com/v3/message/all").header("Authorization", getAuthorization()).form(content).execute();
        var body = request.body();
        log.info("小米推送结果:{}", body);
    }

    /**
     * 注册id推送
     *
     * @param regIdList    注册id列表
     * @param contentModel 内容
     */
    @Override
    public void regId(List<String> regIdList, MessagePushContentModel contentModel) {
        var list = regIdList.stream().map(regId -> {
            var message = buildContent(contentModel);
            var map = new HashMap<String, Object>();
            map.put("message", message);
            map.put("target", regId);
            return map;
        }).toList();
        var content = URLEncoder.encode(JSON.toJSONString(list), StandardCharsets.UTF_8);
        var request = HttpRequest.post("https://api.xmpush.xiaomi.com/v2/multi_messages/regids").header("Authorization", getAuthorization()).form(content).execute();
        var body = request.body();
        log.info("小米推送结果:{}", body);
    }

    /**
     * 别名推送信息
     *
     * @param aliasList    别名列表
     * @param contentModel 内容
     */
    @Override
    public void alias(List<String> aliasList, MessagePushContentModel contentModel) {
        var list = aliasList.stream().map(alias -> {
            var message = buildContent(contentModel);
            var map = new HashMap<String, Object>();
            map.put("message", message);
            map.put("target", alias);
            return map;
        }).toList();
        var content = URLEncoder.encode(JSON.toJSONString(list), StandardCharsets.UTF_8);
        var request = HttpRequest.post("https://api.xmpush.xiaomi.com/v2/multi_messages/aliases").header("Authorization", getAuthorization()).form(content).execute();
        var body = request.body();
        log.info("小米推送结果:{}", body);
    }

    /**
     * 构造推送内容
     *
     * @param contentModel 内容
     * @return 推送内容
     */
    private Map<String, Object> buildContent(MessagePushContentModel contentModel) {
        var map = new HashMap<String, Object>();
        map.put("payload", contentModel.getContent());
        map.put("restricted_package_name", pushXiaomiProperty.getPackageName());
        map.put("title", contentModel.getTitle());
        map.put("description", contentModel.getContent());
        var extra = new HashMap<String, Object>();
        extra.put("notify_effect", 1);
        map.put("extra", extra);
        return map;
    }

    /**
     * 获取认证请求头
     *
     * @return 认证请求头
     */
    private String getAuthorization() {
        return "key=" + pushXiaomiProperty.getSecret();
    }

    /**
     * 推送发送的名称
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "xiaomi";
    }

}
